package cn.xinfei.xdecision.common.model.component.decisiontable.vo;

import cn.xinfei.xdecision.common.model.component.IgnoredParamCheck;
import cn.xinfei.xdecision.common.model.component.decisiontable.DecisionTablesVersion;
import cn.xinfei.xdecision.common.model.component.strategyout.StrategyOutput;
import cn.xinfei.xdecision.common.utils.enums.enginex.ComponentVersionStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class DecisionTablesVersionVo extends DecisionTablesVersion {
    private List<DecisionTablesDetailVo> leftDetailVo;//左侧决策表详情

    private List<DecisionTablesDetailVo> topDetailVo;//顶部决策表详情

    private DecisionTablesResultVo resultSet;//结果集（二维数组表）
    private List<StrategyOutput> strategyOutputList;//输出字段

    /**
     * @see ComponentVersionStatusEnum
     */
    @ApiModelProperty("版本状态描述")
    @IgnoredParamCheck
    private String statusDesc;

    @IgnoredParamCheck
    private String versionNoDesc;

    public void recursionNodeType() {
        if (!CollectionUtils.isEmpty(this.leftDetailVo)) {
            this.leftDetailVo.forEach(this::recursionNodeType);
        }
        if (!CollectionUtils.isEmpty(this.topDetailVo)) {
            this.topDetailVo.forEach(this::recursionNodeType);
        }
    }

    public void recursionNodeType(DecisionTablesDetailVo decisionTablesDetailVo) {
        List<DecisionTablesDetailVo> childrenDetails = decisionTablesDetailVo.getChildren();
        if (CollectionUtils.isEmpty(childrenDetails)) {
            decisionTablesDetailVo.setNodeType(2);
        } else {
            decisionTablesDetailVo.setNodeType(1);
            childrenDetails.forEach(this::recursionNodeType);
        }
    }

    public void recurrenceSetIdIsNull() {
        if (!CollectionUtils.isEmpty(this.leftDetailVo)) {
            this.leftDetailVo.forEach(this::recurrenceDetail);
        }
        if (!CollectionUtils.isEmpty(this.topDetailVo)) {
            this.topDetailVo.forEach(this::recurrenceDetail);
        }
        if (!CollectionUtils.isEmpty(this.strategyOutputList)) {
            this.strategyOutputList.forEach(x -> this.setId(null));
        }
        this.setId(null);
    }

    private void recurrenceDetail(DecisionTablesDetailVo decisionTablesDetailVo) {
        if (!CollectionUtils.isEmpty(decisionTablesDetailVo.getConditionList())) {
            decisionTablesDetailVo.getConditionList().forEach(x -> {
                x.setId(null);
                x.setDetailId(null);
            });
        }
        decisionTablesDetailVo.setId(null);
        List<DecisionTablesDetailVo> children = decisionTablesDetailVo.getChildren();
        if (!CollectionUtils.isEmpty(children)) {
            children.forEach(x -> {
                x.setId(null);
                x.setVersionId(null);
                recurrenceDetail(x);
            });
        }
    }

}
